package com.yuanfeng.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuanfeng.base.entity.BaseExpressEntity;
import com.yuanfeng.base.entity.BaseKdniaoExpresssEntity;
import com.yuanfeng.base.mapper.BaseExpressMapper;
import com.yuanfeng.base.mapper.BaseKdniaoExpresssMapper;
import com.yuanfeng.base.service.BaseExpressService;
import com.yuanfeng.base.vo.ExpressQueryByShopDpexpressVO;
import com.yuanfeng.base.vo.ManageExpressQueryVO;
import com.yuanfeng.commoms.constant.CommonType;
import com.yuanfeng.commoms.dto.base.BaseExpressDTO;
import com.yuanfeng.commoms.exception.BizCodeEnume;
import com.yuanfeng.commoms.util.ParseTokenUtils;
import com.yuanfeng.commoms.util.ResponseResult;
import com.yuanfeng.commoms.util.TimeStampUtil;
import com.yuanfeng.commoms.vo.base.KdniaoExpressVO;
import com.yuanfeng.commoms.vo.user.UserInfoVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 *
 */
@Service
public class BaseExpressServiceImpl extends ServiceImpl<BaseExpressMapper, BaseExpressEntity>
    implements BaseExpressService{

    @Resource
    private BaseKdniaoExpresssMapper baseKdniaoExpresssMapper;

    @Override
    public ResponseResult queryNoToken() {
        //查询快递公司 列表
        List<BaseExpressEntity> expressList = this.baseMapper.selectList(new LambdaQueryWrapper<BaseExpressEntity>()
                .eq(BaseExpressEntity::getExpressStatus, CommonType.commonStatus.YES.getCode()));
        return ResponseResult.success(expressList);
    }

    @Override
    public ResponseResult queryExpressList(Map<String, Object> map) {
        if (null != map && null != map.get("token")) {
            //校验token
            String token = map.get("token").toString();
            UserInfoVO userInfo = ParseTokenUtils.getPlatformUser(token);
            if (null != userInfo) {
                LambdaQueryWrapper<BaseExpressEntity> query = new LambdaQueryWrapper<>();
                if (map.get("expressStatus") != null) {
                    query.eq(BaseExpressEntity::getExpressStatus, map.get("expressStatus"));
                }
                //查询快递公司 列表
                List<BaseExpressEntity> expressList = this.baseMapper.selectList(query);
                return ResponseResult.success(expressList);
            }
            return ResponseResult.fail(BizCodeEnume.TOKEN_IS_ILLEGAL.getCode(), BizCodeEnume.TOKEN_IS_ILLEGAL.getMsg());
        }
        return ResponseResult.fail(BizCodeEnume.VALID_EXCEPTION.getCode(), BizCodeEnume.VALID_EXCEPTION.getMsg());
    }

    @Override
    public ResponseResult getBaseExpressInfoById(Integer expressId) {
        BaseExpressEntity expressEntity = this.baseMapper.selectById(expressId);
        return ResponseResult.success(expressEntity);
    }

    @Override
    public List<ManageExpressQueryVO> queryNoTokenTwo() {
        List<ManageExpressQueryVO> manageExpressQueryVOS =  this.baseMapper.queryNoTokenTwo();
        return manageExpressQueryVOS;
    }

    @Override
    public ResponseResult addExpress(BaseExpressDTO baseExpressReq) {
        if (null != baseExpressReq && null != baseExpressReq.getToken()) {
            //校验token
            String token = baseExpressReq.getToken();
            UserInfoVO userInfo = ParseTokenUtils.getPlatformUser(token);
            if (null != userInfo) {
                //检验快递公司或者快递公司首字符是否重复
                Integer express = this.baseMapper.selectCount(new LambdaQueryWrapper<BaseExpressEntity>()
                                                .eq(BaseExpressEntity::getExpressName,baseExpressReq.getExpressName())
                                                .or()
                                                .eq(BaseExpressEntity::getExpressPinyin,baseExpressReq.getExpressPinyin()));
                if (express > 0) {
                    return ResponseResult.fail(BizCodeEnume.FIRST_CHARACTER_NOT_REPEAT);
                }
                //新增快递公司
                this.baseMapper.addExpress(baseExpressReq);
                return ResponseResult.success(BizCodeEnume.OPERATION_SUCCESSFUL);
            }
            return ResponseResult.fail(BizCodeEnume.TOKEN_IS_ILLEGAL);
        }
        return ResponseResult.fail(BizCodeEnume.VALID_EXCEPTION);
    }

    @Override
    public ResponseResult updateExpress(BaseExpressDTO baseExpressReq) {
        if (null != baseExpressReq && null != baseExpressReq.getToken()) {
            //校验token
            String token = baseExpressReq.getToken();
            UserInfoVO userInfo = ParseTokenUtils.getPlatformUser(token);
            if (null != userInfo) {
                //检验快递公司或者快递公司首字符是否重复
                Integer count =  this.baseMapper.selectCount(new LambdaQueryWrapper<BaseExpressEntity>()
                        .ne(BaseExpressEntity::getExpressId,baseExpressReq.getExpressId())
                        .and(wp -> wp.eq(BaseExpressEntity::getExpressName,baseExpressReq.getExpressName())
                        .or()
                        .eq(BaseExpressEntity::getExpressPinyin,baseExpressReq.getExpressPinyin()))
                        );
                if (count > 0) {
                    return ResponseResult.fail(BizCodeEnume.FIRST_CHARACTER_NOT_REPEAT);
                }
                //修改快递公司
                this.baseMapper.updateExpress(baseExpressReq);
                return ResponseResult.success();
            }
            return ResponseResult.fail(BizCodeEnume.TOKEN_IS_ILLEGAL);
        }
        return ResponseResult.fail(BizCodeEnume.VALID_EXCEPTION);
    }

    @Override
    public ResponseResult deleteExpress(BaseExpressDTO baseExpressReq) {
        if (null != baseExpressReq && null != baseExpressReq.getToken()) {
            //校验token
            String token = baseExpressReq.getToken();
            UserInfoVO userInfo = ParseTokenUtils.getPlatformUser(token);
            if (null != userInfo) {
                //删除快递公司
                this.baseMapper.deleteById(baseExpressReq.getExpressId());
                return ResponseResult.success();
            }
            return ResponseResult.fail(BizCodeEnume.TOKEN_IS_ILLEGAL);
        }
        return ResponseResult.fail(BizCodeEnume.VALID_EXCEPTION);
    }

    @Override
    public ResponseResult queryByShopDpexpress(Map<String, Object> paraMap) {
        if (paraMap.get("shopId") == null || paraMap.get("shopId").equals("")) {
            return ResponseResult.fail(2,"请求参数不能为空");
        }
        Integer shopId = (Integer) paraMap.get("shopId");
        List<ExpressQueryByShopDpexpressVO> list = this.baseMapper.newQueryByShopDpexpress(shopId);
        for(ExpressQueryByShopDpexpressVO se:list) {
            int num=se.getExpressId();
            KdniaoExpressVO querykdniaoexpress = this.baseMapper.newQuerykdniaoexpress(num);
            if(null != querykdniaoexpress) {
                se.setExpressName(querykdniaoexpress.getExpressName());
                se.setSendSite(querykdniaoexpress.getExpressPinyin());
            }
        }
        return ResponseResult.success(list);
    }

    @Override
    public ResponseResult updateShopDpexpress(Map<String, Object> map) {
        map.put("time",TimeStampUtil.getNowDate());
        boolean isUpdateSuccess = this.baseMapper.updateShopDpexpress(map);
        return ResponseResult.success(isUpdateSuccess);
    }

    @Override
    public ResponseResult deleteDpexpressParam(Map<String, Object> paraMap) {
        if (paraMap.get("Id") == null || paraMap.get("Id").equals("")) {
            return ResponseResult.fail(2,"请求参数不能为空");
        }
        boolean delkdniao = this.baseMapper.deletcShopDpexpress(Integer.valueOf(paraMap.get("Id").toString()));
        return ResponseResult.success(delkdniao);
    }

    @Override
    public ResponseResult insertshopDpexpress(Map<String, Object> paraMap) {
        int insertshopdpexpress = this.baseMapper.insertShopDpexpress(paraMap);
        return ResponseResult.success(insertshopdpexpress);
    }

    @Override
    public ResponseResult queryKdiniaoExperss(Map<String, Object> paraMap) {
        if (paraMap.get("Id") == null || paraMap.get("Id").equals("")) {
            return ResponseResult.fail(2,"请求参数不能为空");
        }
        BaseKdniaoExpresssEntity expresssEntity = baseKdniaoExpresssMapper.selectById(Integer.valueOf(paraMap.get("Id").toString()));
        return ResponseResult.success(expresssEntity);
    }

    @Override
    public String queryBaseExpressById(Integer orderShippingExpressId) {
        return this.baseMapper.selectById(orderShippingExpressId).getExpressName();
    }
}




